package com.nc.delivery_project.client.users;

import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.cellview.client.DataGrid;
import com.nc.delivery_project.client.composite.PagingDataGrid;
import com.nc.delivery_project.shared.proxy.UserProxy;

/**
 * Created by miroshnik on 12/29/2014.
 */
public class UserPagingDataGrid extends PagingDataGrid<UserProxy> {

    @Override
    public void initTableColumns(DataGrid<UserProxy> dataGrid, ColumnSortEvent.ListHandler<UserProxy> sortHandler) {
        Column<UserProxy, String> idCol = createIdCol();
        Column<UserProxy, String> emailCol = createEmailCol();
        Column<UserProxy, String> roleCol = createRoleCol();

        dataGrid.addColumn(idCol, "ID");
        dataGrid.addColumn(emailCol, "Email");
        dataGrid.addColumn(roleCol, "Role");
    }

    private Column<UserProxy, String> createRoleCol() {
        Column<UserProxy, String> col = new Column<UserProxy, String>(new TextCell()) {
            @Override
            public String getValue(UserProxy object) {
                return object.getRole().getTitle();
            }
        };

        return col;
    }

    private Column<UserProxy, String> createEmailCol() {
        Column<UserProxy, String> col = new Column<UserProxy, String>(new EditTextCell()) {
            @Override
            public String getValue(UserProxy object) {
                return object.getEmail();
            }
        };

        return col;
    }

    private Column<UserProxy, String> createIdCol() {
        Column<UserProxy, String> col = new Column<UserProxy, String>(new TextCell()) {
            @Override
            public String getValue(UserProxy object) {
                return String.valueOf(object.getId());
            }
        };

        return col;
    }
}
